package com.th.supcom.pds.auditprescription.server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.auditprescription.client.service.AuditPrescripRpc;
import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.common.enums.IsNeedCheck;
import com.th.supcom.pds.domain.vo.PatientInfo;
import com.th.supcom.pds.domain.vo.PdsSupplyDetailVO;
import com.th.supcom.pds.domain.vo.PdsSupplyVO;
import com.th.supcom.pds.domain.vo.Result;
import com.th.supcom.pds.internal.api.AuditPrescService;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.internal.client.model.ResultModel;

/**
 * AuditPrescrip 控制层
 * 
 * @author yzhang
 * 
 */
public class AuditPrescripController extends GwtController implements AuditPrescripRpc
{
    /**
     * 日志记录器.
     */
    public static final Logger s_logger = LoggerFactory.getLogger (AuditPrescripController.class);
    /**
     * add by yzhang
     */
    private static final long serialVersionUID = 1L;

    private AuditPrescService m_auditPrescService;

    public AuditPrescripController ()
    {
    }

    public void setAuditPrescService (final AuditPrescService auditPrescService)
    {
        this.m_auditPrescService = auditPrescService;
    }

    @Override
    public String checkPresc (PdsSupplyModel model, String checkFlag) throws UiException
    {
        s_logger.info ("调用checkPresc方法进行处方检查,入参:pdsSupplyModel:{},checkFlag:{}", new Object[]
        { model, checkFlag });
        PdsSupplyVO pdsSupplyVO = ConvertUtils.convert (PdsSupplyVO.class, model);
        Map <String, Object> map = new HashMap <String, Object> ();
        map.put ("recprtId", model.getPrescId ());
        map.put ("execNumber", "22");
        map.put ("empIdExec", model.getUserIdCheck ());
        map.put ("empNameExec", model.getUserNameCheck ());
        map.put ("orgIdExec", model.getOrgIdExec ());
        map.put ("orgNameExec", model.getOrgIdExecName ());
        map.put ("execAction", checkFlag);
        map.put ("execNotes", model.getCheckMemo ());
        return m_auditPrescService.checkPresc (pdsSupplyVO);
    }

    @Override
    public ResultModel findPrescDetailInfoByPrescId (PdsSupplyModel model) throws UiException
    {
        s_logger.info ("调用findPrescDetailInfoByPrescId处方查询，入参：PdsSupplyModel:{}", new Object[]
        { model });
        PdsSupplyVO pdsSupplyVO = ConvertUtils.convert (PdsSupplyVO.class, model);
        Result result = m_auditPrescService.findPrescDetailByPrescNo (pdsSupplyVO);

        if (result == null || null == result.getPdsSupplyVOs ())
        {
            throw new BusinessException ("无该处方信息");
        }

        if (IsNeedCheck.UnNeed.getEnumCode ().equals (result.getPdsSupplyVOs ().get (0).getCheckFlag ()))
        {
            throw new BusinessException ("该处方不需要审核");
        }

        if (null != result.getPdsSupplyVOs ().get (0).getCheckDatetime ())
        {
            throw new BusinessException ("该处方未审核");
        }

        PatientInfo patientInfo = result.getPatientInfo ();
        List <PdsSupplyVO> pdsSupplyVOs = result.getPdsSupplyVOs ();
        List <PdsSupplyDetailVO> pdsSupplyDetailVOs = result.getPdsSupplyDetailVos ();
        ResultModel resultModel = new ResultModel ();
        PatientInfoModel patientInfoModel = ConvertUtils.convert (PatientInfoModel.class, patientInfo);
        List <PdsSupplyModel> pdsSupplyModels = ConvertUtils.convert (PdsSupplyModel.class, pdsSupplyVOs);
        List <PdsSupplyDetailModel> pdsSupplyDetailModels = ConvertUtils.convert (PdsSupplyDetailModel.class, pdsSupplyDetailVOs);
        resultModel.setPatientInfoModel (patientInfoModel);
        resultModel.setPdsSupplyDetailModels (pdsSupplyDetailModels);
        resultModel.setPdsSupplyModels (pdsSupplyModels);
        return resultModel;
    }

}
